import { getTokenInfo } from './../../utils/storage';
const initialState = {
  auth: getTokenInfo(),
  info: {} as userInfoType,
  // 声明状态
  profile: {} as ProfileType,
};

// state状态的属性type
export type Auth = {
  token: string;
  refresh_token: string;
};
export type userInfoType = {
  id: string;
  name: string;
  photo: string;
  art_count: number;
  follow_count: number;
  fans_count: number;
  like_count: number;
};
export type ProfileType = {
  id: string;
  photo: string;
  name: string;
  mobile: string;
  gender: number;
  birthday: string;
  intro: string;
};

// ActionType 拓展类型
export type userAction =
  | {
      type: 'user/login';
      payload: Auth;
    }
  | {
      type: 'save/userInfo';
      payload: userInfoType;
    }
  | {
      type: 'save/userProfile';
      payload: ProfileType;
    }
  | {
      type: 'save/updatePhoto';
      payload: Partial<ProfileType>;
    };

// eslint-disable-next-line import/no-anonymous-default-export
export default (
  state = initialState,
  action: userAction
): typeof initialState => {
  switch (action.type) {
    // 拓展分支逻辑
    case 'user/login':
      return {
        ...state,
        auth: action.payload,
      };
    case 'save/userInfo':
      return {
        ...state,
        info: action.payload,
      };
    case 'save/userProfile':
      return {
        ...state,
        profile: action.payload,
      };
    case 'save/updatePhoto':
      return {
        ...state,
        profile: {
          ...state.profile,
          ...action.payload,
        },
      };
    default:
      return state;
  }
};
